/**
 * 患者主信息管理初始化
 */
var CdcPatient = {
    id: "CdcPatientTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CdcPatient.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '患者主索引', field: 'patientCode', align: 'center', valign: 'middle', sortable: true},
        {title: '电子健康码', field: 'healthCode', align: 'center', valign: 'middle', sortable: true},
        {title: '姓名', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '性别', field: 'gender', align: 'center', valign: 'middle', sortable: true},
        {title: '国籍', field: 'nationality', align: 'center', valign: 'middle', sortable: true},
        {title: '民族', field: 'nation', align: 'center', valign: 'middle', sortable: true},
        {title: '出生日期', field: 'birthdate', align: 'center', valign: 'middle', sortable: true},
        {title: '年龄', field: 'age', align: 'center', valign: 'middle', sortable: true},
        {title: '年龄月', field: 'ageMonth', align: 'center', valign: 'middle', sortable: true},
        {title: '年龄天', field: 'ageDay', align: 'center', valign: 'middle', sortable: true},
        {title: '证件类型', field: 'cardType', align: 'center', valign: 'middle', sortable: true},
        {title: '证件号码', field: 'cardValue', align: 'center', valign: 'middle', sortable: true},
        {title: '籍贯', field: 'nativePlace', align: 'center', valign: 'middle', sortable: true},
        {title: '户籍所在地详细描述', field: 'nativeAddress', align: 'center', valign: 'middle', sortable: true},
        {title: '现住址详细描述', field: 'nowAddress', align: 'center', valign: 'middle', sortable: true},
        {title: '工作单位', field: 'company', align: 'center', valign: 'middle', sortable: true},
        {title: '电话', field: 'telephone', align: 'center', valign: 'middle', sortable: true},
        {title: '邮箱', field: 'email', align: 'center', valign: 'middle', sortable: true},
        {title: '联系人', field: 'contactor', align: 'center', valign: 'middle', sortable: true},
        {title: '联系人电话', field: 'contactorPhone', align: 'center', valign: 'middle', sortable: true},
        {title: '职业', field: 'career', align: 'center', valign: 'middle', sortable: true},
        {title: '血型ABO', field: 'bloodAbo', align: 'center', valign: 'middle', sortable: true},
        {title: '血型Rh', field: 'bloodRh', align: 'center', valign: 'middle', sortable: true},
        {title: '业务数据类型', field: 'dataId', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:CdcPatient.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:CdcPatient.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
CdcPatient.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcPatient.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增患者主信息
 */
CdcPatient.add = function () {
    var index = Sys.open('新增患者主信息',Sys.backendPath + '/cdc/cdcpatient/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看患者主信息详情
 */
CdcPatient.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('患者主信息详情',Sys.backendPath + '/cdc/cdcpatient/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除患者主信息
 */
CdcPatient.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdcpatient/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcPatient.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcPatientId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除患者主信息?",operation);
    }
};

/**
 * 查询患者主信息列表
 */
CdcPatient.search = function () {
    var queryData = {};
    queryData['healthCode'] = $("#healthCode").val();
    queryData['name'] = $("#name").val();
    queryData['gender'] = $("#gender").val();
    queryData['nationality'] = $("#nationality").val();
    queryData['nation'] = $("#nation").val();
    queryData['birthdate'] = $("#birthdate").val();
    queryData['age'] = $("#age").val();
    queryData['ageMonth'] = $("#ageMonth").val();
    queryData['ageDay'] = $("#ageDay").val();
    queryData['cardType'] = $("#cardType").val();
    queryData['cardValue'] = $("#cardValue").val();
    queryData['nativePlace'] = $("#nativePlace").val();
    queryData['nativeAddress'] = $("#nativeAddress").val();
    queryData['nowAddress'] = $("#nowAddress").val();
    queryData['company'] = $("#company").val();
    queryData['telephone'] = $("#telephone").val();
    queryData['email'] = $("#email").val();
    queryData['contactor'] = $("#contactor").val();
    queryData['contactorPhone'] = $("#contactorPhone").val();
    queryData['career'] = $("#career").val();
    queryData['bloodAbo'] = $("#bloodAbo").val();
    queryData['bloodRh'] = $("#bloodRh").val();
    queryData['dataId'] = $("#dataId").val();
    queryData['isActive'] = $("#isActive").val();
    CdcPatient.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = CdcPatient.initColumn();
    var table = new BSTable(CdcPatient.id, Sys.backendPath + "/cdc/cdcpatient/list", defaultColunms);
    table.setPaginationType("server");
    CdcPatient.table = table.init();
});
